package com.atomos.strategy.emission
{
	import flash.geom.Point;
	
	import com.atomos.utils.RandomUtils;	

	/**
	 * Emits particles with a randomly modified rate.
	 * 
	 * @author Cédric Néhémie
	 * 
	 */
	public class ByRandomRateEmission extends ByRateEmission implements EmissionStrategy
	{
		protected var _nRandom : Number;
		protected var _nOriginalRate : Number; 
		
		public function ByRandomRateEmission( type : Class, position : Point = null, rate : Number = 1, rand : Number = 0 )
		{
			super( type, position, rate );
			setRandom( rand );
			
			_nOriginalRate = _nRate;
		}
		
		public override function prepareEmission( time : Number ) : void
		{
			var t : Number = _nTimeRest + time;
			
			setRate( _nOriginalRate + RandomUtils.balance( _nRandom ) );
			
			_nParticlesRest = ( t - ( _nTimeRest = ( t % _nTimeStep ) ) ) / _nTimeStep;
		}
		
		public override function getRate():Number
		{
			return _nOriginalRate;
		}
		
		public function getRandom() : uint
		{
			return _nRate;
		}
		
		public function setRandom( rand : uint ) : void
		{
			if( isNaN ( rand ) ) rand = 0;
			
			_nRandom = rand;	
		}
	}
}